<?php

namespace app\storeapi\controller;
use app\storeapi\model\Order AS OrderModel;
use app\storeapi\model\Express as ExpressModel;

/**
 * 订单管理控制器
 * Class Region
 * @package app\store\controller
 */
class Order extends Controller
{
    /**
     * 订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function orderList()
    {
        $param = $this->postData();
        $model = new OrderModel;
        $data = $model->getList($param);
        return $this->renderSuccess($data);

    }

    /**
     * 订单详情
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function orderDetail()
    {
        $param = $this->postData();
        // 订单详情
        $data = OrderModel::detail($param['order_id']);
        return $this->renderSuccess($data);

    }

    /**
     * 获取物流列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function expressList()
    {
        $param = $this->postData();
        // 物流公司列表
        $data = ExpressModel::getAll();
        return $this->renderSuccess($data);

    }

    /**
     * 确认发货
     * @param $order_id
     * @param $express_id 物流公司id
     * @param $express_no 物流单号
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function orderDelivery()
    {
        $param = $this->postData();
        $model = OrderModel::detail($param['order_id']);
        if(!$model) return $this->renderError('订单信息错误，请刷新重试');
        if ($model->delivery($param)) {
            return $this->renderSuccess([],'发货成功');
        }
        return $this->renderError($model->getError() ?: '发货失败');
    }



    /**
     * 审核：用户取消订单
     * @param $order_id
     * @return array|bool
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function confirmCancel($order_id)
    {
        $param = $this->postData();
        $model = OrderModel::detail($param['order_id']);
        if(!$model) return $this->renderError('订单信息错误，请刷新重试');

        if ($model->confirmCancel($param)) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }



}



